package com.demo.springboot.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration // 是一个自动配置的类
public class WebMvcConfig implements WebMvcConfigurer {

    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        final InterceptorRegistration demo = registry.addInterceptor(new DemoInterceptor());
        // 拦截除/demo之外的所有请求
        demo.addPathPatterns("/**").excludePathPatterns("/demo");
    }

    // 配置视图和控制器
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {//
        registry.addViewController("/view/demo") // 访问路径
                .setViewName("demo"); // 模板文件名称
    }

    // 配置静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
        // 上传目录处理
        registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/tmp/upload/");
    }
    // 跨域

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowedOrigins("*");
    }
}
